package com.ye.service.impl;

import com.ye.domain.User;
import com.ye.mapper.UserMapper;
import com.ye.service.UserService;
import com.ye.utils.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> queryAll() {
        return userMapper.queryAll();
    }

    @Override
    public User queryUserById(Integer id) {
        return userMapper.queryUserById(id);
    }



    @Override
    public void updatePassword(String username, String password) {
        userMapper.updatePassword(username,password);
    }

    @Override
    public void updateState(Integer id, String state) {
        userMapper.updateState(id, state);
    }

    @Override
    public String getSalt(String username) {
        return userMapper.getSalt(username);
    }

//    登录
    @Override
    public User loginAuth(String username, String password) {
//        获取数据库加密盐值
        String salt = userMapper.getSalt(username);
//        获取加密后的密码
        String dmd5Password = Md5Util.dmd5Password(password, salt);
        return userMapper.loginAuth(username, dmd5Password);
    }

//    注册
    @Override
    public void insertUser(User user) {
//        加密后的用户信息
        User Md5user = Md5Util.md5Password(user);
        userMapper.insertUser(Md5user);
    }
}
